/*
 * $Id: Playlists.java 374 2008-04-15 02:59:08Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewInflate;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Playlists extends Activity
{
	private ListView mList;

	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.lastfm);

		mList = (ListView)findViewById(android.R.id.list);

		mIntents = new Intent[] {
			new Intent(this, LastfmTopArtists.class),
			new Intent(this, LastfmTopAlbums.class),
			null,
			null,
			null,
			null,
			null,
		};

		ListAdapter adapter =
		  new SubIndicatorAdapter(this, mOptions, mIntents);

		mList.setAdapter(adapter);
		mList.setOnItemClickListener(mOnItemClick);
	}

	private static final String[] mOptions =
	{
		"Top Artists",
		"Top Albums",
		"Top Tracks",
		"Recently Played",
		"Lost & found",
		"Newest",
		"Friends",
	};

	private Intent[] mIntents;

	private OnItemClickListener mOnItemClick = new OnItemClickListener()
	{
		public void onItemClick(AdapterView av, View v, int pos, long id)
		{
			Intent intent = mIntents[pos];
			
			if (intent == null)
			{
				Toast.makeText(Playlists.this, mOptions[pos] + ": Not implemented due to time constraints!",
				  Toast.LENGTH_SHORT).show();
				
				return;
			}

			startActivity(intent);
		}
	};
	
	private static class SubIndicatorAdapter extends BaseAdapter
	{
		private ViewInflate mInflate;
		private String[] mText;
		private Intent[] mIntents;
		
		public SubIndicatorAdapter(Context ctx, String[] text, Intent[] intents)
		{
			super();
			
			mText = text;
			mIntents = intents;
			
			mInflate = ViewInflate.from(ctx);
		}

		public View getView(int pos, View convert, ViewGroup parent)
		{
			View v;

			if (convert == null)
				v = mInflate.inflate(R.layout.playlist_item, null, null);
			else
				v = convert;

			TextView text = (TextView)v.findViewById(R.id.text);
			ImageView sub = (ImageView)v.findViewById(R.id.sub);

			text.setText(mText[pos]);
			
			if (mIntents[pos] == null)
			{
				text.setEnabled(false);
				sub.setEnabled(false);
			}

			return v;
		}

		public int getCount()
		{
			return mText.length;
		}

		public Object getItem(int pos)
		{
			return pos;
		}

		public long getItemId(int pos)
		{
			return pos;
		}
	}
}
